/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.javscaz.javscaztools.fechas;

import java.util.Calendar;
import java.util.Date;
import org.javscaz.javscaztools.mensajeria.Msg;

/**
 *
 * @author avbravo
 */
public class ManejoFechas {
  public static String FormatearFecha(Date date, String formatoFecha) {
        try {
            if (date == null) {
                return "";
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int day = cal.get(Calendar.DATE);
            int month = cal.get(Calendar.MONTH) + 1;
            int year = cal.get(Calendar.YEAR);
            String sfecha;
            String sdia, smonth, syear;

            if (day < 10) {
                sdia = "0" + String.valueOf(day);
            } else {
                sdia = String.valueOf(day);
            }
            if (month < 10) {
                smonth = String.valueOf(month);
            } else {

                smonth = String.valueOf(month);
            }
            syear = String.valueOf(year);

            formatoFecha = formatoFecha.trim();
            if (formatoFecha.equals("dd/MM/aaaa")) {
                return sdia + "/" + month + "/" + syear;
            }

            if (formatoFecha.equals("MM/dd/aaaa")) {
                return smonth + "/" + sdia + "/" + syear;
            }

            if (formatoFecha.equals("aaaa/MM/dd")) {
                return syear + "/" + smonth + "/" + sdia;
            }
            if (formatoFecha.equals("dd/aaaa/MM")) {
                return sdia + "/" + syear + "/" + month;
            }
            if (formatoFecha.equals("MM/aaaa/dd")) {
                return smonth + "/" + syear + "/" + sdia;
            }
            if (formatoFecha.equals("aaaa/dd/MM")) {
                return syear + "/" + sdia + "/" + smonth;
            }


        } catch (Exception ex) {
            Msg.error(ex, "FormatearFecha()");
        }
        return "";
    }  
}
